<template>
    <div>
        <div class="header-title">
            <span @click="$router.go(-1)" class="mint-toast-icon mintui mintui-back"></span>
            <span v-show="password.titleType==1?true:false">修改交易密码</span>
            <span v-show="password.titleType==2?true:false">设置交易密码</span>
            <span></span>
        </div>
        <div class="num-list">
            <div class="input-area">
                <span>支付密码</span>
                <input
                    v-model="password.newPassword"
                    style="text-align:right"
                    type="text"
                    name
                    id
                    placeholder="请输入6位数密码"
                />
            </div>
            <div class="input-area">
                <span>确认密码</span>
                <input
                    v-model="password.confirmPassword"
                    style="text-align:right"
                    type="text"
                    name
                    id
                    placeholder="请再次输入密码"
                />
            </div>
            <mt-button size="normal" class="submit-btn" @click="modifyDealPassword">确定</mt-button>
        </div>
    </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
    data() {
        return {
            password: {
                token: JSON.parse(this.$store.state.userMsg).token,
                newPassword: "",
                confirmPassword: "",
                verificationCode: this.$route.query.verifyCode,
                type: "0",
                passwordType: "1",
                titleType: this.$route.query.titleType
            }
        };
    },
    methods: {
        modifyDealPassword() {
            var checkDealPassword = /^\d{6}$/;
            if (
                this.password.newPassword == "" ||
                this.password.confirmPassword == ""
            ) {
                Toast({ message: "请输入完整的密码" });
                return false;
            }
            if (!checkDealPassword.test(this.password.newPassword)) {
                Toast({ message: "请输入六位存数字密码" });
                return false;
            }
            if (this.password.newPassword != this.password.confirmPassword) {
                Toast({ message: "两次输入的密码不一致" });
                return false;
            }
            //修改交易密码
            if (this.password.titleType == 1) {
                this.$axios
                    .post(this.address.updatePassword, this.password)
                    .then(res => {
                        console.log(res.data);
                        if (res.data.code == 200) {
                            Toast({ message: "交易密码修改成功" });
                            this.$router.push({ name: "safetyCenter" });
                        } else {
                            Toast({ message: res.data.msg });
                        }
                    });
            }
            //设置交易密码
            if (this.password.titleType == 2) {   
                var params = {
                    token:this.password.token,
                    newPassword :this.password.newPassword,
                    confirmPassword :this.password.confirmPassword ,
                    type:'0'
                }       
                this.$axios
                    .post(this.address.setTransactionPassword,params)
                    .then(res => {
                        console.log(res.data);
                        if (res.data.code == 200) {
                            Toast({ message: "交易密码设置成功" });
                            this.$router.push({ name: "safetyCenter" });
                        }else if(res.data.code == 239){
                            Toast({ message: res.data.msg });
                            this.$router.push({ name: "safetyCenter" });
                        } else {
                            Toast({ message: res.data.msg });
                        }
                    });
            }
        }
    }
};
</script>
<style lang="less" scoped>
.num-list {
    padding: 15px;
    box-sizing: border-box;
}
</style>
